package shujia.day04.homework;

public class BubblingDemo {
    public static void main(String[] args) {
        int[] arr = {23, 45, 12, 37, 15};
        System.out.println("冒泡排序前：");
        printIntArray(arr);

        for (int i = 1; i < arr.length; i++) {  // 外层循环表示排序的次数
            for (int j = 0; j < arr.length - i; j++) { // 每一次的比较
                if (arr[j] > arr[j + 1]) {
                    int tmp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = tmp;
                }
            }
        }

        System.out.println("冒泡排序后：");
        printIntArray(arr);
    }


    public static void printIntArray(int[] array) {
        for (int i = 0; i < array.length; i++) {
            if (i == 0) {
                System.out.print("[" + array[i] + ",");
            } else if (i == array.length - 1) {
                System.out.println(array[i] + "]");
            } else {
                System.out.print(array[i] + ",");
            }
        }
    }
}
